package com.dd.controller;

import com.dd.entity.Book;
import com.dd.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * ProjectName: springMvc01->Book1Controller
 * Description: TODO：
 * Author: 丘丘
 * DateTime: 2024/12/27 08:41
 */
@Controller
@ResponseBody
@RequestMapping(produces = "application/json;charset=utf-8")
public class Book1Controller {
    // 自动装配Service
    @Autowired
    private BookService bookService;

    // 浏览器要访问 所有图书
    @RequestMapping("/getAllBooks")
    public List<Book> getList() {
        List<Book> bookList = bookService.selectAllBooks();
        return bookList;
    }


    // 根据编号查图书
    @RequestMapping("selectBookById")  // get,delete 都是可以直接通过url传递请求参数  http://www.a.com/selectBookById?id=10
    Book selectBookById(int id) {
        Book book = bookService.selectBookById(id);
        return book;
    }

    ;

    // 根据编号删除图书
    @RequestMapping("deleteBookById")  //http://www.a.com/deleteBookById?id=10
    String deleteBookById(int id) {
        int i = bookService.deleteBookById(id);
        // 直接根据受影响行数，返回是不是成功
        return i > 0 ? "成功" : "失败";
    }

    ;

    // 添加新图书
    @RequestMapping("insertBook")  // 添加，和修改，一般用js直接发送一个json格式的对象给服务器
    String insertBook(@RequestBody  Book book) {
        return bookService.insertBook(book) > 0 ? "成功" : "失败";
    }

    // 修改图书
    @RequestMapping("updateBook")  // {"bookName":"百年"}
    String updateBook(@RequestBody Book book) {
        return bookService.updateBook(book) > 0 ? "成功" : "失败";
    }


    // 根据书名查询图书
    @RequestMapping("selectBookByBookName")
    List<Book> selectBookByBookName(String bookName) {
        return bookService.selectBookByBookName(bookName);
    }
}
